#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

void inital_board(char board[ROWS][COLS], int rows, int cols, char c)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			board[i][j] = c;
		}
	}
}

void display_board(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	printf("\n-------  Mine sweeper  --------\n\n");
	for (i = 0; i <= col; i++)
		printf("%d ", i);
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);
		for (j = 1; j <= col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
	printf("\n-------  Mine sweeper  --------\n\n");
}

void set_mine(char mine[ROWS][COLS], int row, int col)
{
	int flag = 0;
	while (flag < mine_count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (mine[x][y] == '0')
		{
			flag++;
			mine[x][y] = '1';
		}
	}
}

int Sweep(char mine[ROWS][COLS], int x, int y)
{
	int count = 0;
	int i = 0;
	int j = 0;
	for (i = x - 1; i <= x + 1; i++)
	{
		for (j = y - 1; j <= y + 1; j++)
		{
			if (mine[i][j] == '1')
				count++;
		}
	}
	return count;
}

void Unfold(int x, int y, char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int* w)
{
	int flag = 0;
	int i = 0;
	int j = 0;
	if (x >= 1 && y >= 1 && x <= row && y <= col)
	{
		flag = Sweep(mine, x, y);
		if (flag == 0)
		{
			show[x][y] = ' ';
			(*w)++;
			for (i = x - 1; i <= x + 1; i++)
			{
				for (j = y - 1; j <= y + 1; j++)
				{
					if (show[i][j] == '*')
					{
						Unfold(i, j, mine, show, ROW, COL,w);
					}
				}
			}
		}
		else
		{
			(*w)++;
			show[x][y] = flag + '0';
		}
	}
}

void mark_mine(char show[ROWS][COLS],int *mc)
{
	while (1)
	{
	A:
		printf("Do you want to mark mine?:>(Y/N)");
		char c = 0;
		getchar();
		scanf("%c", &c);
		if (c == 'Y')
		{
			printf("Please enter the coordinates you want to mark:");
			int x = 0;
			int y = 0;
			scanf("%d%d", &x, &y);
			if (show[x][y] == '*')
			{
				show[x][y] = '@';
				system("cls");
				display_board(show, ROW, COL);
				printf("The count of mines is %d\n", --(*mc));
			}
			else
			{
				printf("This coordinate has already been marked\n");
				goto A;
			}
		}
		else
			return;
	}
}

void get_mine(char show[ROWS][COLS], char mine[ROWS][COLS], int row, int col)
{
	int mc = mine_count;
	int win = 0;
	while (win < (ROW * COL - mine_count))
	{
		mark_mine(show,&mc);
		printf("Please enter the coordinates you want to troubleshoot:>");
		int x = 0;
		int y = 0;
		scanf("%d%d", &x, &y);
		system("cls");
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("It's a pity that you were blown up");
				display_board(show, ROW, COL);
				display_board(mine, ROW, COL);
				break;
			}
			else if (show[x][y] != '*')
			{
				printf("This coordinate has already been troubleshooted\n");
				display_board(show, ROW, COL);
			}
			else
			{
				int ret = Sweep(mine, x, y);
				
				if (ret > 0)
				{
					win++;
					show[x][y] = ret + '0';
				}
				else
				{
					Unfold(x, y, mine, show, ROW, COL, &win);
				}
				display_board(show, ROW, COL);
			}
		}
		else
			printf("Input error, please enter again!\n");
	}
	if (win == ROW * COL - mine_count)
	{
		printf("Congratulations, successful minesweeper!\n");
		display_board(mine, ROW, COL);
	}
}